import type { Bullets } from "./bullets";
import {Enemys} from "./enemy"
import {MyPlane} from "./myPlane"
import { crashTest } from "./commom"

export class GameEngine {
  el:HTMLDivElement;
  public bullets : Array<Bullets> = [];
  public enemys : Array<Enemys> = [];
  static instanceObj:GameEngine;
  level:number = 0;
  constructor(){
    this.el = document.querySelector("#main_body") as HTMLDivElement;
    this.init();
  }
  static instance() {
    if (GameEngine.instanceObj == null) {
      GameEngine.instanceObj = new GameEngine();
    }
    return GameEngine.instanceObj;
  }

  init() {

    this.el.addEventListener("click",(e:Event)=>{
      let target:HTMLElement = e.target as HTMLElement;
      if(target.nodeName != "LI") return;
      this.level = Number(target.getAttribute("value"));
      this.start();
    })
  }

  start() {
    this.el.innerHTML = "";
    this.loading(()=>{
      this.el.innerHTML="";
      MyPlane.instance();
      this.controller();
      MyPlane.instance().fire();
      this.buildEnemys();
      this.loopTest();
    });

  }

  loading(callback:Function) {
    let logo = document.createElement("div");
    logo.classList.add("logo");
    this.el.appendChild(logo);
    let loading = document.createElement("div");
    loading.classList.add("loading");
    this.el.appendChild(loading);

    let loadingIndex = 0;
    let timer = setInterval(()=>{
      loadingIndex++;
      loading.style.backgroundImage = `url(/src/assets/images/loading${(loadingIndex%3)+1}.png)`;
      if (loadingIndex == 6){
        clearInterval(timer);
        if(callback && typeof callback == "function"){
          callback();
        }
      }
    },400);
  }

  controller() {
    window.addEventListener("keydown",(e:KeyboardEvent)=>{
      if(e.keyCode == 37) {
        let mX = MyPlane.instance().el.offsetLeft - 20;
        if (mX <= 0){
          mX = 0;
        }
        MyPlane.instance().el.style.left = mX + "px";
      }

      if(e.keyCode == 39) {
        let mX = MyPlane.instance().el.offsetLeft + 20;
        let maxWidth = GameEngine.instance().el.offsetWidth - MyPlane.instance().el.offsetWidth;
        if (mX >= maxWidth){
          mX = maxWidth;
        }
        MyPlane.instance().el.style.left = mX + "px";
      }

      if(e.keyCode == 38) {
        let mY = MyPlane.instance().el.offsetTop - 20;
        if (mY <= 0){
          mY = 0;
        }
        MyPlane.instance().el.style.top = mY + "px";
      }

      if(e.keyCode == 40) {
        let mY = MyPlane.instance().el.offsetTop + 20;
        let maxHeight = GameEngine.instance().el.offsetHeight - MyPlane.instance().el.offsetHeight;
        if (mY >= maxHeight){
          mY = maxHeight;
        }
        MyPlane.instance().el.style.top = mY + "px";
      }
    })
  }

  buildEnemys() {
    setInterval(()=>{
      new Enemys();
    },1000)
  }

  loopTest() {
    let timer = setInterval(()=> {
      for(const b in this.bullets) {
        for(const e in this.enemys) {
          let isCrash = crashTest(this.bullets[b].el,this.enemys[e].el);
          if (isCrash) {
            this.enemys[e].hurt();
            this.bullets[b].remove();
            break;
          }
        }
      }

      for(const e in this.enemys) {
        let isCrash = crashTest(MyPlane.instance().el,this.enemys[e].el);
        if(isCrash) {
          clearInterval(timer);
          alert("游戏结束！");
          window.location.reload();
          break;
        }
      }
    })
  }
}